package net.cyue.homework.student.management.servlet.user;


import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpSession;
import net.cyue.homework.student.management.http.HTTPServlet;
import net.cyue.homework.student.management.http.annotation.GetMethod;
import net.cyue.homework.student.management.http.annotation.PostMethod;
import net.cyue.homework.student.management.util.ResponseUtil;
import net.cyue.nest.nestjava.common.annotation.Request;
import net.cyue.nest.nestjava.common.annotation.Response;
import net.cyue.nest.nestjava.common.interfaces.http.IHTTPRequest;
import net.cyue.nest.nestjava.common.interfaces.http.IHTTPResponse;

@WebServlet(name = "UserLogoutServlet", value = "/action/user/logout")
public class UserLogoutServlet extends HTTPServlet {
    @GetMethod
    @PostMethod
    public void logout(
        @Request IHTTPRequest request,
        @Response IHTTPResponse response
    ) {
        HttpSession session = request.getSession();
        if (session.getAttribute("userID") == null) {
            ResponseUtil.showMessage(response, "你还没有登录");
            return;
        }
        session.removeAttribute("userID");
        session.removeAttribute("username");
        ResponseUtil.waitBackHome(response, 2000, "退出成功");
    }
}


